package cc.magicjson.example.demo.chatgpt;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

// 枚举表示参赛者的选择
enum Choice {
    FIRST, SECOND, THIRD
}

// Monty Hall游戏类
class MontyHallGame {

    private Choice initialChoice;
    private Choice carLocation;
    private Choice openedDoor;

    // 构造函数初始化游戏
    public MontyHallGame() {
        initialChoice = randomChoice();
        carLocation = randomChoice();
        openDoor();
    }

    // 随机选择一扇门
    private static Choice randomChoice() {
        List<Choice> choices = new ArrayList<>();
        choices.add(Choice.FIRST);
        choices.add(Choice.SECOND);
        choices.add(Choice.THIRD);

        Collections.shuffle(choices);
        return choices.get(0);
    }

    // 进行Monty Hall游戏
    public void play(boolean changeChoice) {
        if (changeChoice) {
            switchDoor();
        }
    }

    // 判断是否赢得游戏
    public boolean isWinner() {
        return initialChoice == carLocation;
    }

    // 主持人打开一扇门
    private void openDoor() {
        List<Choice> doors = new ArrayList<>();
        doors.add(Choice.FIRST);
        doors.add(Choice.SECOND);
        doors.add(Choice.THIRD);

        doors.remove(initialChoice);
        if (initialChoice != carLocation) {
            doors.remove(carLocation);
        }

        openedDoor = doors.get(0);
    }

    // 改变选择
    private void switchDoor() {
        List<Choice> choices = new ArrayList<>();
        choices.add(Choice.FIRST);
        choices.add(Choice.SECOND);
        choices.add(Choice.THIRD);

        choices.remove(initialChoice);
        choices.remove(openedDoor);
        initialChoice = choices.get(0);
    }
}

// Monty Hall模拟器类
class MontyHallSimulatorByChatGPT3Point5 {

    private static final int NUM_SIMULATIONS = 100000;

    public static void main(String[] args) {
        int changeWins = simulateMontyHall(true);
        int stayWins = simulateMontyHall(false);

        double changeWinPercentage = calculatePercentage(changeWins, NUM_SIMULATIONS);
        double stayWinPercentage = calculatePercentage(stayWins, NUM_SIMULATIONS);

        DecimalFormat format = new DecimalFormat("#.##");

        System.out.println("改变选择赢得的百分比: " + format.format(changeWinPercentage) + "%");
        System.out.println("保持选择赢得的百分比: " + format.format(stayWinPercentage) + "%");
    }

    // 模拟Monty Hall问题
    private static int simulateMontyHall(boolean changeChoice) {
        int wins = 0;

        for (int i = 0; i < NUM_SIMULATIONS; i++) {
            MontyHallGame game = new MontyHallGame();
            game.play(changeChoice);

            if (game.isWinner()) {
                wins++;
            }
        }

        return wins;
    }

    // 计算百分比
    private static double calculatePercentage(int wins, int total) {
        return ((double) wins / total) * 100;
    }
}
